﻿using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using PaySharp.Alipay;
using PaySharp.Allinpay;
using PaySharp.Core;
using PaySharp.Core.Mvc;
using PaySharp.Unionpay;
using PaySharp.Wechatpay;
using Serilog;

namespace PaySharp.Demo_Net_
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.File($"{AppDomain.CurrentDomain.BaseDirectory}\\logs\\log.txt")
                         .Enrich.WithWebApiRouteTemplate()
                         .Enrich.WithWebApiActionName()
                         .CreateLogger();

            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(a =>
            {
                a.MapHttpAttributeRoutes();

                a.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}",
                    defaults: new { id = RouteParameter.Optional }
                );
            });
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            var containerBuilder = new ContainerBuilder();
            PaySharpConfig.Register(typeof(MvcApplication), containerBuilder, a =>
             {
                 var gateways = new Gateways();
                 //gateways.RegisterAlipay();

                 var alipayMerchant = new Alipay.Merchant
                 {
                     AppId = "2021001173692408",
                     NotifyUrl = "http://localhost:61378/Notify",
                     ReturnUrl = "http://localhost:61378/Notify",
                     AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy67jMg93FVWi8L4+dJuSITwWO8pTZhREDbfs1KQPeXZAcNf9BD6P4XfeH5HqxMhwsjUzLuW+aFEVItUu8LqG/zpChex2xiY6lAbt22Y+NqY2YRDZrFKCD5gSltL8pz5d0TTQgW0ec+/czjMwF/Ye/s2SCCpGJtY9pV75tUVqMhtSKCPUdSLbQIC7z8qAw6SarJpO5ibnjkCZaeB2SG4L2UvfbSTFo7lYMRej0VJjvoJ8VR/YJaUJ1vRxVyoGh0rsaqD+GroC+B3h1iv/cXZ7CJY1za0DEeI1VfOb7P4JP6NsSsPz5bA28pxOa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nylxpqFSdW44uazUqLBlOeGBYdILFECgYAihCurseJh9JD7bquaglyYFsyMEzfA/bZf5ffY6C1l3z5byUrWPt+MOmJnbhIYd/8Kwj/RHh/PdJ+j64VWSQAbBgNITW7JZWsteAr2TzSVbsr944lERvBjvKzmqspHV2GA9KWs2Yux3jHVdQl13hLKmo5IYsHEZ5d96/aCvWzqwQKBgHNyaNl8EftzsRUoUmh7nJSRvbSPqYWSg5uwTm0qrp5+Vg1h0q3yopkkLmzX/G50P+e+284IxAA/HMk/kCLCb4/la/ymfOzDFzr6bmEYzQ3Xgvn+7QtzN8JKSg/JQzK8ALQlXtmJkLRnpBpnFKjZUGUjlV2rpDc3hMe8syczVs5xAoGAUHFSaM3et0aIijvL49ma/nVLrTUnRKDYnUz/m1hAS58fDx9hC6q/meVbG993Zkak909GxMXsEOKsyMPOEhcUyi9MZ0t+JKBEha5ZDaNHEcF2RSl5kgFy9g/TLM9yXyDmgd1yUVJZQ6jzQc9EBSw/oCCQG8WDROYMhFcSiCHgg/4=",


                 };

                 var wechatpayMerchant = new Wechatpay.Merchant
                 {
                     AppId = "wx2428e34e0e7dc6ef",
                     MchId = "1233410002",
                     Key = "e10adc3849ba56abbe56e056f20f883e",
                     AppSecret = "51c56b886b5be869567dd389b3e5d1d6",
                     SslCertPath = AppDomain.CurrentDomain.BaseDirectory + "Certs/apiclient_cert.p12",
                     SslCertPassword = "1233410002",
                     NotifyUrl = "http://localhost:61378/Notify"
                 };

                 var unionpayMerchant = new Unionpay.Merchant
                 {
                     AppId = "777290058110048",
                     CertPwd = "000000",
                     CertPath = AppDomain.CurrentDomain.BaseDirectory + "Certs/acp_test_sign.pfx",
                     NotifyUrl = "http://localhost:61378/Notify",
                     ReturnUrl = "http://localhost:61378/Notify"
                 };

                 var allinpayMerchant = new Allinpay.Merchant
                 {
                     AppId = "00000051",
                     MchId = "990581007426001",
                     Key = "allinpay888",
                     NotifyUrl = "http://localhost:61338/Notify"
                 };

                 gateways.Add(new AlipayGateway(alipayMerchant)
                 {
                     GatewayUrl = "https://openapi.alipay.com"
                 });
                 gateways.Add(new WechatpayGateway(wechatpayMerchant));
                 gateways.Add(new UnionpayGateway(unionpayMerchant)
                 {
                     GatewayUrl = "https://gateway.test.95516.com"
                 });
                 gateways.Add(new AllinpayGateway(allinpayMerchant)
                 {
                     GatewayUrl = "https://test.allinpaygd.com"
                 });

                 return gateways;
             });
        }
    }
}
